<?php

require_once ('Zend/Form.php');

class Form_Result_AddAdditionalQuestion extends Zend_Form {
	
	private $url;
	private $update;
	private $data = array();
	
	
	public function __construct($options = null,$url,$data,$update) {
		parent::__construct ($options);
		
		$jqueryData = '';
				
		
		$this->setAjaxParams($url,$data,$update);
		
		if(!is_array($this->data) || empty($this->url) || empty($this->update)){
			throw new Zend_Form_Exception('Set ajax params');
		}
		
		$this->setMethod('post');
		
		$questionText = new Zend_Form_Element_Text('questionText');
		$questionText->addFilter('StripTags')
		->addFilter('StringTrim')
		->setDecorators(array('ViewHelper'))
		->setAttrib('onKeyPress','if(event.keyCode == 13){return false}');
		
		foreach($data as $key => $value){
			$jqueryData .= $key . ':' . $value . ','; 
		}
		
		$jqueryData = rtrim($jqueryData,',');
		
		
		$jqueryFunction = '$.post("' . $this->url . '",{formInput: $("form input:text").val(),' . $jqueryData . '},
						   function(data){
						   		$("' . $this->update . '").html(data);
						   },"html")';
		
		$submit = new Zend_Form_Element_Button('submitQuestion');
		$submit->setLabel('Pridať')
		->setAttrib('onClick',$jqueryFunction)
		->setDecorators(array('ViewHelper'));
		
		$this->addElements(array($questionText,$submit));
	
	}
	
	private function setAjaxParams($url,$data,$update){		
		$this->url = $url;
		$this->data = $data;
		$this->update = $update;
	}
}